﻿@{
    ViewBag.Title = T("Admin.Configuration.DeliveryTimes").Text;
}
<div class="section-header">
    <div class="title">
		<i class="far fa-clock"></i>
        <span>@T("Admin.Configuration.DeliveryTimes")</span>
    </div>
</div>

<div>
	@(Html.Telerik().Grid<DeliveryTimeModel>()
		.Name("delivery-time-grid")
        .DataKeys(x =>
        {
            x.Add(y => y.Id);
        })
		.Columns(columns =>
		{
			columns.Bound(x => x.Name);
			columns.Bound(x => x.ColorHexValue)
				.ClientTemplate("<span class='color-container'><span class='color' style='background:<#= ColorHexValue #>'>&nbsp;</span></span><span><#= ColorHexValue #></span>")
				.Width(200);
			columns.Bound(x => x.DisplayOrder)
				.Centered()
				.Width(200);
			columns.Bound(x => x.IsDefault)
				.ClientTemplate(Html.SymbolForBool("IsDefault"))
				.Centered()
				.Width(200);
            columns.Command(commands =>
            {
                commands.Custom("edit-delivery-time").Text(T("Common.Edit"));
                commands.Delete().Localize(T);
            }).Width(180)
            .HtmlAttributes(new { align = "right", @class = "omega" });
		})
        .ToolBar(commands => commands.Template(DeliveryTimeGridCommands))
		.DataBinding(dataBinding => dataBinding.Ajax()
            .Select("List", "DeliveryTime")
            .Delete("DeleteDeliveryTime", "DeliveryTime"))
		.PreserveGridState()
		.EnableCustomBinding(true)
	)
</div>

@helper DeliveryTimeGridCommands(Grid<DeliveryTimeModel> grid)
{
	<button type="submit" id="btnAddNew" name="btnAddNew" class="btn" onclick="javascript:openPopup('@(Url.Action("CreateDeliveryTimePopup", "DeliveryTime", new { btnId = "btnRefresh" }))'); return false;">
		<i class="fa fa-plus"></i>
		<span>@T("Admin.Configuration.DeliveryTimes.AddNew")</span>
	</button>
	<input type="submit" id="btnRefresh" name="btnRefresh" class="d-none" />
}

<script>
	$(function() {
		$('#btnRefresh').click(function () {
			$('#delivery-time-grid').data('tGrid').ajaxRequest();
			return false;
		});

		$('#delivery-time-grid').on('click', '.t-grid-edit-delivery-time', function (e) {
			e.preventDefault();
			var tr = $(this).closest('tr');
			var id = $('#delivery-time-grid').data('tGrid').dataItem(tr).Id;
			var href = "@Url.Content("~/Admin/DeliveryTime/EditDeliveryTimePopup/")" + id + "?btnId=btnRefresh";
			
			openPopup(href);
			return false;
		});
	});
</script>
